﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;
using DevExpress.XtraEditors;

namespace ArcMap_AddIn_Tools
{
    public partial class points_distance_Form : Form
    {
        private IMxDocument mxDocument = ArcMap.Document;
        private IFeatureClass first_point_fc = null;
        private IFeatureClass second_point_fc = null;
        private List<IFeature> first_points = new List<IFeature>();
        private List<IFeature> second_points = new List<IFeature>();

        public points_distance_Form()
        {
            InitializeComponent();
        }

        private void points_distance_Form_Load(object sender, EventArgs e)
        {
            LoadFeatureLayerToCombox();
        }

        private void LoadFeatureLayerToCombox()
        {
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, null);
            if (fcs.Count == 0)
                return;
            this.comboBox_First_PointFC.BeginUpdate();
            this.comboBox_Second_PointFC.BeginUpdate();
            for (int i = 0; i < fcs.Count; i++)
            {
                string name = fcs[i].Name;
                this.comboBox_First_PointFC.Items.Add(name);
                this.comboBox_Second_PointFC.Items.Add(name);
            }
            this.comboBox_First_PointFC.EndUpdate();
            this.comboBox_Second_PointFC.EndUpdate();
        }


        private void comboBox_first_PointFC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string first_point_layer_name = this.comboBox_First_PointFC.Text;
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, first_point_layer_name);
            if (fcs.Count == 0)
                return;
            IFeatureLayer first_point_layer = fcs[0];
            first_point_fc = first_point_layer.FeatureClass;

            IQueryFilter QueryFilter = new QueryFilterClass();
            QueryFilter.SubFields = "*";
            QueryFilter.WhereClause = string.Format("");
            first_points = FeatureClassWrapper.GetFeaturesFromFeatureClass(first_point_fc, QueryFilter);
        }

        private void comboBox_secong_PointFC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string second_layer_name = this.comboBox_Second_PointFC.Text;
            IMap map = mxDocument.FocusMap;
            if (map.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(map, second_layer_name);

            if (fcs.Count == 0)
                return;
            IFeatureLayer second_layer = fcs[0];
            second_point_fc = second_layer.FeatureClass;

            IQueryFilter QueryFilter = new QueryFilterClass();
            QueryFilter.SubFields = "*";
            QueryFilter.WhereClause = string.Format("");
            second_points = FeatureClassWrapper.GetFeaturesFromFeatureClass(second_point_fc, QueryFilter);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox_First_PointFC.Text == "" || this.comboBox_Second_PointFC.Text == "")
            {
                return;
            }

        }

        public void AddListBox_ImportInfo(ListBoxControl listBoxControl, string txt, bool addNewLine)
        {
            listBoxControl.Items.Add(txt);
            if (addNewLine == true)
            {
                listBoxControl.Items.Add("");
            }
            listBoxControl.SelectedIndex = listBoxControl.Items.Count;
            Application.DoEvents();
        }

        private void button_X_All_Click(object sender, EventArgs e)
        {
            listBoxControl1.Items.Clear();
            string txt = "";
            for (int i = 0; i < first_points.Count; i++)
            {
                IPoint first_Point = first_points[i].Shape as IPoint;
                IPoint second_Point = second_points[i].Shape as IPoint;
                double x_sub = Math.Round(first_Point.X, 6) - Math.Round(second_Point.X, 6);
                string text = string.Format("{0}-{1}={2}", Math.Round(first_Point.X, 6), Math.Round(second_Point.X, 6), x_sub);
                AddListBox_ImportInfo(this.listBoxControl1, text, false);
                txt += string.Format("{0}-{1}={2}", Math.Round(first_Point.X, 6), Math.Round(second_Point.X, 6), x_sub) + "\r\n";
            }
        }

        private void button_X_mean_Click(object sender, EventArgs e)
        {

        }

        private void button_Y_All_Click(object sender, EventArgs e)
        {
            listBoxControl1.Items.Clear();
            string txt = "";
            for (int i = 0; i < first_points.Count; i++)
            {
                IPoint first_Point = first_points[i].Shape as IPoint;
                IPoint second_Point = second_points[i].Shape as IPoint;
                double y_sub = Math.Round(first_Point.Y, 6) - Math.Round(second_Point.Y, 6);
                string text = string.Format("{0}-{1}={2}", Math.Round(first_Point.Y, 6), Math.Round(second_Point.Y, 6), y_sub);
                AddListBox_ImportInfo(this.listBoxControl1, text, false);
                txt += string.Format("{0}-{1}={2}", Math.Round(first_Point.Y, 6), Math.Round(second_Point.Y, 6), y_sub) + "\r\n";
            }
        }

        private void button_Y_mean_Click(object sender, EventArgs e)
        {

        }

        private void button_point_All_Click(object sender, EventArgs e)
        {

        }

        private void button_point_mean_Click(object sender, EventArgs e)
        {

        }
    }
}
